package com.icode.gulimall.ware.lisetener;

import com.alibaba.fastjson.TypeReference;
import com.icode.common.constant.OrderStatusEnum;
import com.icode.common.to.OrderTo;
import com.icode.common.to.mq.StockDetailTo;
import com.icode.common.to.mq.StockLockedTo;
import com.icode.common.utils.R;
import com.icode.gulimall.ware.dao.WareSkuDao;
import com.icode.gulimall.ware.entity.WareOrderTaskDetailEntity;
import com.icode.gulimall.ware.entity.WareOrderTaskEntity;
import com.icode.gulimall.ware.feign.OrderFeignService;
import com.icode.gulimall.ware.service.WareOrderTaskDetailService;
import com.icode.gulimall.ware.service.WareOrderTaskService;
import com.icode.gulimall.ware.service.WareSkuService;
import com.icode.gulimall.ware.vo.OrderVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Author: my_code
 * @Email: my_code@foxmail.com
 * @Create: 2021-02-08 17:58
 */
@Service
@RabbitListener(queues = "stock.release.queue")
public class StockReleaseListener {

    @Autowired
    WareSkuService wareSkuService;

    @RabbitHandler
    public void handlerStockLockedRelease(StockLockedTo stockLockedTo, Message message, Channel channel) throws IOException {

        try {
            wareSkuService.unlockStock(stockLockedTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }

    @RabbitHandler
    public void handlerOrderCloseRelease(OrderTo orderTo, Message message, Channel channel) throws IOException {

        try {
            wareSkuService.unlockStock(orderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }

}
